package com.reder.practice.service.impl;

import com.reder.practice.bean.LoginInfo;
import com.reder.practice.bean.Manager;
import com.reder.practice.bean.extend.LoginInfoExtend;
import com.reder.practice.bean.extend.ManagerExtend;
import com.reder.practice.common.dto.ServerResponse;
import com.reder.practice.common.enums.state.CommonState;
import com.reder.practice.common.enums.state.ManagerState;
import com.reder.practice.common.event.ManagerLoginEvent;
import com.reder.practice.common.util.CodeUtil;
import com.reder.practice.common.util.PageUtil;
import com.reder.practice.common.util.SqlResultUtil;
import com.reder.practice.dao.ManagerDao;
import com.reder.practice.service.ManagerService;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 管理员业务实现类
 *
 * @author Fish
 * <p>Email: fishinlove@163.com</p>
 * <p>created by 2019/09/24 10:02:17</p>
 */
@Service
public class ManagerServiceImpl implements ManagerService {

    @Resource
    private ApplicationContext context = null;

    @Resource
    private ManagerDao managerDao = null;

    // 检查这三个参数是否有一些是空白的
    private boolean isSomeOfThemAbsent(String username, String realName, String password) {
        return CodeUtil.isBlank(username) || CodeUtil.isBlank(realName) ||
                CodeUtil.isBlank(password);
    }

    // 返回参数不完整的响应
    private ServerResponse responseOfUncompletedParameter() {
        return ServerResponse.response(CommonState.PARAMETER_UNCOMPLETED);
    }

    @Override
    public ServerResponse login(String username, String password) {

        // 检查参数完整性
        if (CodeUtil.isBlank(username) || CodeUtil.isBlank(password)) {
            return responseOfUncompletedParameter();
        }

        // 查询用户信息
        Manager manager = managerDao.selectByUsername(username);
        if (manager == null) {
            return ServerResponse.response(ManagerState.USERNAME_OR_PASSWORD_IS_WRONG);
        }

        // 校验密码
        if (CodeUtil.md5(password).equals(manager.getPassword())) {
            // 用户名或密码正确
            context.publishEvent(new ManagerLoginEvent(manager));
            return ServerResponse.response(CommonState.SUCCESS, loginInfoOf(manager));
        }

        // 用户名或密码不正确
        return ServerResponse.response(ManagerState.USERNAME_OR_PASSWORD_IS_WRONG);
    }

    // 将敏感信息屏蔽掉
    private void removeSensitiveInfo(Manager manager) {
        if (manager != null) {
            manager.setPassword(null);
        }
    }

    // 包装用户登录信息
    private LoginInfo loginInfoOf(Manager manager) {

        // 敏感信息不公开
        removeSensitiveInfo(manager);

        // 装载用户 token 和用户信息
        return LoginInfoExtend.newLoginInfo(manager, ManagerExtend.tokenOf(manager));
    }

    @Override
    public ServerResponse isUsernameExisted(String username) {

        // 判断参数是否完整
        if (CodeUtil.isBlank(username)) {
            return responseOfUncompletedParameter();
        }

        int count = managerDao.countByUsername(username);
        if (count > 0) {
            // 用户昵称已经存在
            return ServerResponse.response(ManagerState.USERNAME_IS_EXISTED);
        }

        // 用户昵称不存在
        return ServerResponse.response(CommonState.SUCCESS);
    }

    @Override
    public ServerResponse addManager(String username, String realName, String password) {

        // 检查参数完整性
        if (isSomeOfThemAbsent(username, realName, password)) {
            return responseOfUncompletedParameter();
        }

        // 检查用户昵称是否存在
        ServerResponse response = isUsernameExisted(username);
        if (!response.isSuccess()) {
            return response;
        }

        // 执行数据操作
        return SqlResultUtil.checkSqlResult(
                managerDao.insert(ManagerExtend.newManager(username, realName, CodeUtil.md5(password))),
                ManagerState.ADD_FAILED
        );
    }

    @Override
    public ServerResponse listManagers(int pageNumber, int sizeOfOnePage) {
        PageUtil.toPage(pageNumber, sizeOfOnePage);
        return ServerResponse.response(CommonState.SUCCESS, PageUtil.pageInfo(
                managerDao.listManagers()
        ));
    }

    @Override
    public ServerResponse removeManager(long id) {
        return SqlResultUtil.checkSqlResult(
                managerDao.deleteByPrimaryKey(id),
                ManagerState.REMOVE_FAILED
        );
    }
}
